package org.zxp.thread.dead;

import org.junit.Test;

/**
 * @program: zxp-thread-test
 * @description: ${description}
 * @author: X-Pacific zhang
 * @create: 2019-05-30 17:28
 **/
public class DeadBlockTest {
    public static void main(String[] args){
        Object o1 = new Object();
        Object o2 = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (o1){
                try { Thread.sleep(500); } catch (InterruptedException e) {}
                synchronized (o2){
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (o2){
                try { Thread.sleep(500); } catch (InterruptedException e) {}
                synchronized (o1){
                }
            }
        });

//        Lock lock1 = new ReentrantLock();
//        Lock lock2 = new ReentrantLock();
//        Thread t1 = new Thread(() -> {
//                lock1.lock();
//                try { Thread.sleep(500); } catch (InterruptedException e) {}
//                lock2.lock();
//                lock2.unlock();
//                lock1.unlock();
//        });
//
//        Thread t2 = new Thread(() -> {
//            lock2.lock();
//            try { Thread.sleep(500); } catch (InterruptedException e) {}
//            lock1.lock();
//            lock1.unlock();
//            lock2.unlock();
//        });
        t1.start();t2.start();
    }

    @Test
    public void deadSuspend() throws InterruptedException {
        Thread thread = new Thread(() -> {
            synchronized (DeadBlockTest.class){
                System.out.println("1");
                Thread.currentThread().suspend();
                System.out.println("2");
            }
        });
        thread.start();
        while(true){
            System.out.println(thread.getState());
            Thread.sleep(500);
        }
//        synchronized (DeadBlockTest.class){
//            thread.resume();
//        }
    }
}
